package com.sqsf.servlet;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "TestServlet", value = "/test")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取应用上下文对象 因为其内存储着spring对象
        ServletContext servletContext = request.getServletContext();
        //WebApplicationContextUtils是spring提供的 从上下文对象中获取spring容器对象的工具类
        WebApplicationContext spring = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        //调用方法获取交由spring容器管理的指定对象
        System.out.println(spring.getBean("user"));
    }
}
